//
//  SYMessageTool.m
//  FaceRecognition
//
//  Created by 薛子 on 2018/6/25.
//  Copyright © 2018年 Sylar. All rights reserved.
//

#import "SYMessageTool.h"

@implementation SYMessageTool

static FMDatabase *_db;

+(void)initialize{
   
   //获取文件路径
   NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
   
//   path = [path stringByAppendingPathComponent:USERID];
   
   NSFileManager *manager = [NSFileManager defaultManager];
   BOOL tag,flag;
   if (![manager fileExistsAtPath:path isDirectory:&flag]) {
      
      if (!flag) {
         
         NSError *err;
         tag = [manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&err];
         NSLog(@"err=%@",err);
      }
      
   }
   
   
   path = [path stringByAppendingPathComponent:@"message.db"];
   
   NSLog(@"path = %@", path);
   
   _db = [FMDatabase databaseWithPath:path];
   
   if (![_db open]) {
      
      NSLog(@"数据库打开失败");
   }
   
     [_db executeUpdate:@"create table if not exists messageList_BPush (msgid integer primary key autoincrement,userid text,title text,description text,msgtitle text,name text,logo text,type integer,moveto integer,infoid text,color text,url text,jointime text,readstate integer,classid text,scid text)"];
}

+ (BOOL)instertBPustMessage:(SY_Activity_Model *)messageModel{
   
   NSString * infoid = messageModel.infoid;
   NSInteger type = messageModel.type;
   NSInteger moveto = messageModel.moveto;
   NSString *title = messageModel.title;
   NSString *description = messageModel.description2;
   NSString *name = messageModel.name;
   NSString *logo = messageModel.logo;
   NSString *color = messageModel.color;
   NSString *url = messageModel.url;
   NSString *msgtitle = messageModel.msgtitle;
   NSString *jointime = messageModel.jointime;
   NSInteger readState = messageModel.readState;
   NSString * classID = messageModel.classid;
   NSString * scID = messageModel.scid;
   
   //删除重复数据
    NSString *sqls = [NSString stringWithFormat:@"delete from messageList_BPush where userid = \"%@\" and infoid = \"%@\" and description = \"%@\"" ,USERID,infoid,description];

    [_db executeUpdate:sqls];
   
   NSLog(@"sqls = %@",sqls);
   
   NSString *sql = [NSString stringWithFormat:@"insert into messageList_BPush(infoid,type,moveto,title,description,name,logo,userid,color,url,msgtitle,jointime,readstate,classid,scid) values(\"%@\",%ld,%ld,\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",%ld,\"%@\",\"%@\");",infoid,(long)type,(long)moveto,title,description,name,logo,USERID,color,url,msgtitle,jointime,(long)readState,classID,scID];
      
      NSLog(@"sql===%@",sql);
      
      BOOL result = [_db executeUpdate:sql];
      
      NSLog(@"result===%d",result);
      
      return result;

}


+(void)cleanDisk:(SY_Activity_Model *)model{
   
   NSString * infoID = model.infoid;
   
      NSString * sql = [NSString stringWithFormat:@"delete from messageList_BPush where userid = \"%@\" and infoid = \"%@\" and description = \"%@\" ",USERID,infoID, model.description2];
   
      BOOL  result = [_db executeUpdate:sql];
   
      if (result) {
         
      NSLog(@"删除成功");
      } else {
         
      NSLog(@"删除失败");
      }
   
}

+(void)updataMessageModel:(SY_Activity_Model *)model{
   
   NSString * infoID = model.infoid;
   
   NSString *sql = [NSString stringWithFormat:@"update messageList_BPush set readstate = 1 where infoid = %@ and userid = %@",infoID,USERID];
   [_db executeUpdate:sql];
   
}
//需要msgid 点到离校
+(void)updataMessageModel2:(SY_Activity_Model *)model{
   
   NSString * infoID = model.infoid;
   NSString * description = model.description2;

   NSString *sql = [NSString stringWithFormat:@"update messageList_BPush set readstate = 1 where userid = %@ and infoid = \"%@\" and description = \"%@\" ",USERID,infoID,description];
   BOOL result =  [_db executeUpdate:sql];
   NSLog(@"----result = %d",result);
   
}
+(NSMutableArray *)getMessageListArrayFrom:(NSInteger)index{
   
   NSString * sql = [NSString stringWithFormat:@"select * from messageList_BPush where userid = %@ order by jointime desc limit %ld, 10",USERID,index];
   
   FMResultSet * resultSet = [_db executeQuery:sql];
   
   NSMutableArray *arr = [NSMutableArray array];
   
   while ([resultSet next]) {
      
      NSString * infoid = [resultSet stringForColumn:@"infoid"];
      NSInteger type = [resultSet intForColumn:@"type"];
      NSInteger moveto = [resultSet intForColumn:@"moveto"];
      NSString * title = [resultSet stringForColumn:@"title"];
      NSString * description = [resultSet stringForColumn:@"description"];
      NSString * name = [resultSet stringForColumn:@"name"];
      NSString * logo = [resultSet stringForColumn:@"logo"];
      NSString * color = [resultSet stringForColumn:@"color"];
      NSString * url = [resultSet stringForColumn:@"url"];
      NSString * msgtitle = [resultSet stringForColumn:@"msgtitle"];
      NSString * jointime = [resultSet stringForColumn:@"jointime"];
      NSInteger readState = [resultSet intForColumn:@"readstate"];
      NSInteger msgid = [resultSet intForColumn:@"msgid"];
      NSString * scID = [resultSet stringForColumn:@"scid"];
      NSString * classId = [resultSet stringForColumn:@"classid"];
      
      SY_Activity_Model * messageModel = [[SY_Activity_Model alloc]init];
      messageModel.infoid = infoid;
      messageModel.type = type;
      messageModel.moveto = moveto;
      messageModel.title = title;
      messageModel.description2 = description;
      messageModel.name = name;
      messageModel.logo = logo;
      messageModel.color = color;
      messageModel.url = url;
      messageModel.msgtitle = msgtitle;
      messageModel.jointime = jointime;
      messageModel.readState = readState;
      messageModel.msgid = msgid;
      messageModel.classid = classId;
      messageModel.scid = scID;
      
      [arr addObject:messageModel];
      
   }
   
   return arr;
   
}

@end
